import processor, re, roomsupport

class Door(roomsupport.Gatekeeper):
    def __init__(self, open, *args, **kwArgs):
        roomsupport.Gatekeeper.__init__(self, *args, **kwArgs)
        self.isopen = open
    def open(self):
        if not self.isopen:
            self.isopen = True
            return "You open the " + self.nameAsKnown() + "."
        else:
            return "It's already open."
    def close(self):
        if self.isopen:
            self.isopen = False
            return "You close the " + self.nameAsKnown() + "."
        else:
            return "It's already closed."
    def canPass(self, fromRoom, toRoom):
        if self.isopen:
            return True
        else:
            raise roomsupport.InvalidRoomError("The door is closed.")

class StuckDoor(Door):
    def __init__(self, blockedMessage, *args, **kwArgs):
        Door.__init__(self, *args, **kwArgs)
        self.blockedMessage = blockedMessage
    def open(self):
        return self.blockedMessage
    def close(self):
        return self.blockedMessage

class Key(processor.Object):
    def __init__(self, object, *args, **kwArgs):
        processor.Object.__init__(self, *args, **kwArgs)
        self.lock = object

class LockedDoor(Door):
    def __init__(self, isLocked, *args, **kwArgs):
        Door.__init__(self, *args, **kwArgs)
        self.islocked = isLocked
    def createKey(self, names):
        return Key(self, names)
    def getKey(self):
        keys = [a for a in processor.me.inventory if isinstance(a, Key) and a.lock == self]
        if len(keys):
            return keys[0]
        else:
            return None
    def open(self):
        if self.islocked:
            return "It's locked."
        else:
            return Door.open(self)
    def unlock(self):
        if self.islocked:
            self.islocked = False
            return "You unlock it with " + self.getKey().nameAsKnown() + "."
        elif self.isopen:
            return "It's already open."
        else:
            return "It's already unlocked."
    def lock(self):
        if not self.islocked:
            self.islocked = True
            return "You lock it with your key."
        elif self.isopen:
            return "It's still open."
        else:
            return "It's already locked."


openRegexp = re.compile("^(open) (.*)$")
closeRegexp = re.compile("^(close) (.*)$")

class OpenClose(object):
    def process(self, command):
        open = openRegexp.search(command)
        close = closeRegexp.search(command)
        if open:
            i = processor.getItem(open.group(2))
            if "isopen" in i.__dict__:
                return i.open()
            else:
                return "It doesn't open or close."
        elif close:
            i = processor.getItem(close.group(2))
            if "isopen" in i.__dict__:
                return i.close()
            else:
                return "It doesn't open or close."

processor.commands.append(OpenClose())



unlockRegexp = re.compile("^(unlock) (.*)$")
lockRegexp = re.compile("^(lock) (.*)$")

class LockUnlock(object):
    def process(self, command):
        unlock = unlockRegexp.search(command)
        lock = lockRegexp.search(command)
        if unlock:
            i = processor.getItem(unlock.group(2))
            if "islocked" in i.__dict__:
                key = i.getKey()
                if not key:
                    return "You have no key which fits the lock."
                return i.unlock()
            else:
                return "It has no lock."
        elif lock:
            i = processor.getItem(lock.group(2))
            if "islocked" in i.__dict__:
                key = i.getKey()
                if not key:
                    return "You have no key which fits the lock."
                return i.lock()
            else:
                return "It has no lock."

processor.commands.append(LockUnlock())

